package com.graph;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;

public class SymbolGraph {

    private ST<String, Integer> st;

    private String keys[];

    private Graph G;

    public SymbolGraph(String stream, String sp) {
        In in = new In(stream);
        st = new ST<>();
        while (in.hasNextLine()) {
            String [] lines = in.readLine().split(sp);
            for (int i = 0; i < lines.length; i++) {
                if (!st.contains(lines[i])) {
                    st.put(lines[i], st.size());
                }
            }
        }

        keys = new String[st.size()];

        for (String key : st.keys()) {
            keys[st.get(key)] = key;
        }

        G = new Graph(st.size());

        in = new In(stream);

        while (in.hasNextLine()) {
            String[] lines = in.readLine().split(sp);
            int v = st.get(lines[0]);
            for (int i = 1; i < lines.length; i++) {
                G.addEdge(v, st.get(lines[i]));
            }
        }
    }

    public boolean contains(String s) { return st.contains(s); }

    public int index (String s) { return st.get(s) ;}

    public String name (int v) { return keys[v]; }

    public Graph G() {return G;}


}
